package mcMST;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
 * Przechowuje informacje o liczbie wierzcholkow grafu i o jego krawedziach
 */
public class Graph {

	private int numOfNodes;
	private int numOfObjectives;

	Double[][][] vertices;

	public Graph(String path) throws IOException {

		BufferedReader in = new BufferedReader(new FileReader(path));     

		// first line is number of nodes
		String[] firstLineSplited = in.readLine().split(" ");
		this.numOfNodes = Integer.parseInt(firstLineSplited[0]);
		this.numOfObjectives = Integer.parseInt(firstLineSplited[1]);

		vertices = new Double[this.numOfNodes][this.numOfNodes][this.numOfObjectives];

		String str;

		int from = 0;
		while ((str = in.readLine()) != null)
		{
			String[] splitedLineStr = str.split(";");

			int to = 0;
			for (String objectivesToTownStr : splitedLineStr) {
				String[] objectivesSplitedStr = objectivesToTownStr.split(",");
				for (int objNum = 0; objNum < this.numOfObjectives; objNum++) {
					vertices[from][to][objNum] = Double.parseDouble(objectivesSplitedStr[objNum]);
				}
				to++;
			}
			from++;
		}
		in.close();
	}

	public int getNumOfNodes() {
		return this.numOfNodes;
	}

	public int getNumOfObjectives() {
		return numOfObjectives;
	}

	public double getEdgeWeight(int from, int to, int objNum) {
		return vertices[from][to][objNum];
	}

	/**
	 * Generate random weights between vertices
	 * 
	 */
	public static void main(String[] args) {

		if (args.length != 3) {
			System.out.println("Argumenty [liczba_wierzcholkow] [liczba_objectives] [plik_docelowy]");
			System.exit(1);
		}

		int numOfNodes = Integer.parseInt(args[0]);
		int numOfObjectives = Integer.parseInt(args[1]);
		Double [][][] vertices = new Double[numOfNodes][numOfNodes][numOfObjectives];
		String targetFilepath = args[2];

		Random rand = new Random();

		for (int objectiveNum = 0; objectiveNum < numOfObjectives; objectiveNum++) {            
			for (int from = 0; from < numOfNodes; from++) {
				for (int to = 0; to < numOfNodes; to++) {
					Double tmp = rand.nextDouble();				
					vertices[from][to][objectiveNum] = tmp;
				}
			}
		}

		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(targetFilepath));

			// pierwsza linia
			out.write(""+numOfNodes+" "+numOfObjectives+"\n");

			// zapisywanie do pliku
			for (int from = 0; from < numOfNodes; from++) {
				for (int to = 0; to < numOfNodes; to++) {
					for (int j = 0; j < numOfObjectives; j++) {
						out.write(""+vertices[from][to][j]);
						if (j != numOfObjectives - 1) {
							out.write(",");
						}
					}
					if (to != numOfNodes - 1) {
						out.write(";");
					}
				}
				out.write("\n");
			}
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("Plik wejsciowy zostal wygenerowany.");
	}

}
